﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections;


namespace GUI_CLock
{
    class AnalogClockFace : ITimeObserver
    {
        //The three Hands of the analog clock
        Hand hourHand;
        Hand minuteHand;
        Hand secondHand;

        Line hour;
        Line minute;
        Line second;

        private ITimeSubject timemodel;
        Canvas canvas;
        Point center;

        float radius;
        Boolean isRunning;

        ArrayList clockFace;

        //Main Constructor
        public AnalogClockFace(ITimeSubject timemodel, Canvas canvas)
        {
            //Register as observer of time subject
            this.timemodel = timemodel;
            timemodel.RegisterObserver(this);

            this.canvas = canvas;
            clockFace = new ArrayList();

            //Get values of window size
            double x = canvas.Width;
            double y = canvas.Height;

            //define center of the clock
            center = new Point((x/2), (y/2));

            //Define size of the clock
            if (x < y)
            {
                radius = (float)((x / 2) - 10);
            }
            else
            {
                radius = (float)((y / 2) - 10);
            }

            //Generate Hands
            hourHand = new Hand(center, 0, radius, DateTime.Now);
            minuteHand = new Hand(center, 1, radius, DateTime.Now);
            secondHand = new Hand(center, 2, radius, DateTime.Now);


            Start();

        }


        public void TimeUpdate(DateTime dateTime)
        {
            //Only display clock if it is running
            if (isRunning)
            {
                //If there are existing lines for hour, minute and second, remove them from the canvas
                if (hour != null)
                {
                    canvas.Children.Remove(hour);
                }
                if (minute != null)
                {
                    canvas.Children.Remove(minute);
                }
                if (second != null)
                {
                    canvas.Children.Remove(second);
                }

                //update the lines
                hour = hourHand.setTime(dateTime);
                minute = minuteHand.setTime(dateTime);
                second = secondHand.setTime(dateTime);

                //replace lines on the canvas
                canvas.Children.Add(hour);
                canvas.Children.Add(minute);
                canvas.Children.Add(second);
            }
        }

        public void Stop()
        {
            //If there are existing lines for hour, minute and second, remove them from the canvas
            if (hour != null)
            {
                canvas.Children.Remove(hour);
            }
            if (minute != null)
            {
                canvas.Children.Remove(minute);
            }
            if (second != null)
            {
                canvas.Children.Remove(second);
            }

            foreach (Line i in clockFace)
            {
                canvas.Children.Remove(i);
            }

            isRunning = false;
        }

        public void Start()
        {
            isRunning = true;
            if (clockFace.Count < 1)
            {
                DrawClockFace();
            }
            else
            {
                foreach (Line i in clockFace)
                {
                    canvas.Children.Add(i);
                }
            }
            
        }

        private void DrawClockFace()
        {
            Point edge = new Point();
            Point inside = new Point();

            for(int i = 0; i <= 12; i++)
            {
                double tickMark = (i * 360) / 12 * Math.PI / 180;
                edge.X = radius * Math.Cos(tickMark);
                edge.Y = radius * Math.Sin(tickMark);

                inside.X = ((18 * radius) / 20) * Math.Cos(tickMark);
                inside.Y = ((18 * radius) / 20) * Math.Sin(tickMark);

                edge.X = edge.X + center.X;
                edge.Y = edge.Y + center.Y;
                inside.X = inside.X + center.X;
                inside.Y = inside.Y + center.Y;

                Line temp = new Line();
                temp.X1 = edge.X;
                temp.Y1 = edge.Y;
                temp.X2 = inside.X;
                temp.Y2 = inside.Y;

                temp.Stroke = System.Windows.Media.Brushes.Black;
                temp.StrokeThickness = 4;

                clockFace.Add(temp);
            }

            for (int i = 0; i <= 60; i++)
            {
                double tickMark = (i * 360) / 60 * Math.PI / 180;
                edge.X = radius * Math.Cos(tickMark);
                edge.Y = radius * Math.Sin(tickMark);

                inside.X = ((19 * radius) / 20) * Math.Cos(tickMark);
                inside.Y = ((19 * radius) / 20) * Math.Sin(tickMark);

                edge.X = edge.X + center.X;
                edge.Y = edge.Y + center.Y;
                inside.X = inside.X + center.X;
                inside.Y = inside.Y + center.Y;

                Line temp = new Line();
                temp.X1 = edge.X;
                temp.Y1 = edge.Y;
                temp.X2 = inside.X;
                temp.Y2 = inside.Y;

                temp.Stroke = System.Windows.Media.Brushes.Black;
                temp.StrokeThickness = 1;

                clockFace.Add(temp);
            }


            foreach (Line i in clockFace)
            {
                canvas.Children.Add(i);
            }
        }
    }
}
